using FaceCat;
using facecat_avalonia;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace facecat_winui
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page, FCTouchEventCallBack
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainPage()
        {
            this.InitializeComponent();
            onLoad();
        }

        /// <summary>
        /// 是否双击
        /// </summary>
        private bool m_isDoubleClick;

        /// <summary>
        /// 公共方法库
        /// </summary>
        public FCNative m_native;

        /// <summary>
        /// 设备对接
        /// </summary>
        public UWPHost m_host;

        /// <summary>
        /// 配置解析
        /// </summary>
        public FCUIXml m_xml;

        /// <summary>
        /// 重绘事件
        /// </summary>
        /// <param name="sender">调用者</param>
        /// <param name="args">参数</param>
        public void canvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            if (m_host.m_canvas == null)
            {
                m_host.m_canvas = sender;
                sender.PointerPressed += Sender_PointerPressed;
                sender.PointerReleased += Sender_PointerReleased;
                sender.PointerMoved += Sender_PointerMoved;
                sender.PointerWheelChanged += Sender_PointerWheelChanged;
            }
            int width = (int)sender.Size.Width, height = (int)sender.Size.Height;
            FCSize oldSize = m_native.getSize();
            if (width > 0 && height > 0)
            {
                if (oldSize.cx != width || oldSize.cy != height)
                {
                    m_native.setSize(new FCSize(width, height));
                    m_native.update();
                }
            }
            UWPPaint aPaint = m_native.getPaint() as UWPPaint;
            aPaint.m_args = args;
            aPaint.m_canvas = sender;
            FCRect clipRect = new FCRect(0, 0, m_native.getSize().cx, m_native.getSize().cy);
            aPaint.beginPaint(IntPtr.Zero, clipRect, clipRect);
            m_native.onPaint(clipRect);
            aPaint.endPaint();
        }

        /// <summary>
        /// 鼠标滚轮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
        {
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_delta = e.GetCurrentPoint(sender as UIElement).Properties.MouseWheelDelta;
                m_native.onMouseWheel(newTouchInfo);
            }
        }

        /// <summary>
        /// 鼠标松开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(sender as UIElement).Position;
            m_host.m_touchPoint = new FCPoint((int)point.X, (int)point.Y);
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = m_isDoubleClick ? 2 : 1;
                m_native.onMouseUp(newTouchInfo);
            }
        }

        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(sender as UIElement).Position;
            m_host.m_touchPoint = new FCPoint((int)point.X, (int)point.Y);
            if (m_host.allowOperate())
            {
                if (m_native.m_touchDownView != null)
                {
                    FCTouchInfo newTouchInfo = new FCTouchInfo();
                    newTouchInfo.m_firstTouch = true;
                    newTouchInfo.m_buttons = 1;
                    newTouchInfo.m_clicks = 1;
                    m_native.onMouseMove(newTouchInfo);
                }
                else
                {
                    FCTouchInfo newTouchInfo = new FCTouchInfo();
                    m_native.onMouseMove(newTouchInfo);
                }
            }
        }

        /// <summary>
        /// 上次点击时间
        /// </summary>
        public DateTime m_lastMouseDownTime = DateTime.Now;

        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(sender as UIElement).Position;
            m_host.m_touchPoint = new FCPoint((int)point.X, (int)point.Y);
            DateTime nowTime = DateTime.Now;
            long ticks = (DateTime.Now - m_lastMouseDownTime).Ticks;
            m_isDoubleClick = ticks < 10000 * 200;
            m_lastMouseDownTime = nowTime;
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = m_isDoubleClick ? 2 : 1;
                m_native.onMouseDown(newTouchInfo);
            }
        }

        /// <summary>
        /// 加载视图
        /// </summary>
        public void onLoad()
        {
            m_xml = new FCUIXml();
            m_xml.createNative();
            m_native = m_xml.getNative();
            m_native.setPaint(new UWPPaint());
            m_host = new UWPHost();
            //m_host.m_view = this;
            m_native.setHost(m_host);
            m_host.setNative(m_native);
            m_native.setSize(new FCSize(800, 700));
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  <html xmlns=""facecat"">
    <body>
      <div dock=""fill"" backcolor=""rgb(140,192,81)"">
        <div name=""Div"" text=""Div"" location=""368,127"" size=""247,326"" align=""center"" vertical-align=""middle"" backcolor=""rgb(170,178,189)"" cornerradius=""10"">
          <input type=""text"" name=""TextBox"" location=""14,18"" size=""220,45"" cornerradius=""0"" bordercolor=""rgb(0,0,0)"" backcolor=""rgb(42,48,72)"" font=""Default,30.0"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAC"" text=""AC"" location=""14,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAddSub"" text=""+/-"" location=""69,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnMod"" text=""%"" location=""124,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnDivide"" text=""÷"" location=""179,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum7"" text=""7"" location=""14,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum8"" text=""8"" location=""69,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum9"" text=""9"" location=""124,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnMulti"" text=""×"" location=""179,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum4"" text=""4"" location=""14,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum5"" text=""5"" location=""69,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum6"" text=""6"" location=""124,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnSub"" text=""-"" location=""179,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum1"" text=""1"" location=""14,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum2"" text=""2"" location=""69,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum3"" text=""3"" location=""124,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAdd"" text=""+"" location=""179,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum0"" text=""0"" location=""14,261"" size=""110,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnDot"" text=""."" location=""124,261"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnResult"" text=""="" location=""179,261"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
        </div>
      </div>
    </body>
  </html>";
            m_xml.loadXml(xml, null);
            m_native.update();
        }
    }
}
